package love.yuanmengrobot.als.business.util;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/4/30 17:32
 */

import love.yuanmengrobot.als.business.dao.ImportPlainDao;
import love.yuanmengrobot.als.business.dao.impl.ImportPlainDaoImpl;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;


public class ReaderPl {


    public void readPlan(String filePath) {
        //一共三处需要手动修改，可以考虑优化成全自动。
        TrainPlan trainPlans[] = null;
        TrainPlan trainPlan = null;
        String majorName = "信息与计算科学专业";//1、手动改名称
        String grade = "2019";//2、手动改年级
        try {
            InputStream in = null;
//            in = ReaderPl.class.getResourceAsStream("2019培养方案表模板v2.2.xlsx");//11，这里就是取Test的上级根为路径，变成了src路径
            in = new FileInputStream(filePath);


            XSSFWorkbook xw = new XSSFWorkbook(in);
            XSSFSheet xs = xw.getSheetAt(1);//3、手动改表页号，这里翻页就改 String majorName = "数学与应用数学";这里的名字
            String name = filePath.substring(filePath.lastIndexOf("\\")+1,filePath.length());
            grade=name.substring(0,4);
            System.out.println("发现培养方案："+grade+""+xs.getSheetName()+"培养方案");

            majorName=xs.getSheetName();

            trainPlans = new TrainPlan[xs.getLastRowNum()];

            int frontIndex = 0;
            int afterIndexStart = 0;
            int afterIndexEnd = 0;

            for (frontIndex = 1; frontIndex <= xs.getLastRowNum(); frontIndex++) {//行遍历，跳行
                XSSFRow row = xs.getRow(frontIndex);
                trainPlan = new TrainPlan();
                for (int k = 0; k <= row.getLastCellNum(); k++) {//列遍历
                    XSSFCell cell = row.getCell(k);
                    if (cell == null) {
                        if (afterIndexEnd == 0) {
                            afterIndexEnd = frontIndex;
                            System.out.println("发现空" + frontIndex);
                        }
                        continue;
                    }
                    switch (k) {
                        case 0:
                            trainPlan.setCourseType(getValue(cell));
                            if (getValue(cell).equals("课程类别")) {//前半读取完毕，结束前半
//                                System.out.println("课程类别" + frontIndex);
                                afterIndexStart = frontIndex - 1;
                                afterIndexEnd = afterIndexStart + 7;
                            }
//                            if (getValue(cell).equals("实践环节")) {//前半读取完毕，结束前半xu
//                                System.out.println("实践环节" + frontIndex+"$");
//                                afterIndexEnd = frontIndex-1;
//                            }
                            break;
                        case 1:
                            if (getValue(cell).equals(""))
                                trainPlan.setCourseLevel("0");
                            else if (getValue(cell).equals("1.0"))
                                trainPlan.setCourseLevel("1");
                            else
                                trainPlan.setCourseLevel(getValue(cell));
                            break;
                        case 2:
                            trainPlan.setCourseName(getValue(cell));
                            break;
                        case 3:
                            trainPlan.setCredit(getValue(cell));
                            break;
                        case 4:
                            if (!getValue(cell).equals("")) {
                                trainPlan.setPlanCourseTerm("1");
                                trainPlan.setCourseHours(getValue(cell));
                            }
                            break;
                        case 5:

                            if (!getValue(cell).equals("")) {
                                trainPlan.setPlanCourseTerm("2");
                                trainPlan.setCourseHours(getValue(cell));
                            }
                            break;
                        case 6:

                            if (!getValue(cell).equals("")) {
                                trainPlan.setPlanCourseTerm("3");
                                trainPlan.setCourseHours(getValue(cell));
                            }
                            break;
                        case 7:

                            if (!getValue(cell).equals("")) {
                                trainPlan.setPlanCourseTerm("4");
                                trainPlan.setCourseHours(getValue(cell));
                            }
                            break;
                        case 8:

                            if (!getValue(cell).equals("")) {
                                trainPlan.setPlanCourseTerm("5");
                                trainPlan.setCourseHours(getValue(cell));
                            }
                            break;
                        case 9:

                            if (!getValue(cell).equals("")) {
                                trainPlan.setPlanCourseTerm("6");
                                trainPlan.setCourseHours(getValue(cell));
                            }
                            break;
                        case 10:

                            if (!getValue(cell).equals("")) {
                                trainPlan.setPlanCourseTerm("7");
                                trainPlan.setCourseHours(getValue(cell));
                            }
                            break;
                        case 11:

                            if (!getValue(cell).equals("")) {
                                trainPlan.setPlanCourseTerm("8");
                                trainPlan.setCourseHours(getValue(cell));
                            }
                            break;
                        default:
                            break;
                    }
                    trainPlans[frontIndex - 1] = trainPlan;
                }
            }
            System.out.println("全" + frontIndex);
            //输出：
            int i = 0;
            trainPlan = new TrainPlan();
            for (TrainPlan TR : trainPlans) {
                if (TR.getCourseType() == null) continue;
                //输出开始
//                System.out.println(i + TR.getCourseType() + " | " +
//                        TR.getCourseLevel() + " | " +
//                        TR.getCourseName() + " | " +
//                        TR.getCredit() + " | " +
//                        TR.getPlanCourseTerm() + " | " +
//                        TR.getCourseHours()
//                );
                //输出结束
//                if (TR.getCourseType().equals("课程类别") && TR.getCourseLevel().equals("应修学分")){
//                    System.out.println("fax" + i);
//                }
                if (TR.getCourseType().equals("通识选修课")) {
//                    System.out.println("******"+TR.getCourseLevel());
                    trainPlan.setGeneralElective(TR.getCourseLevel());
                }
                if (TR.getCourseType().equals("通识必修课")) {
//                    System.out.println("******"+TR.getCourseLevel());
                    trainPlan.setGeneralRequired(TR.getCourseLevel());
                }
                if (TR.getCourseType().equals("学科基础课")) {
//                    System.out.println("******"+TR.getCourseLevel());
                    trainPlan.setDisciplineBasis(TR.getCourseLevel());
                }
                if (TR.getCourseType().equals("专业基础必修课")) {
//                    System.out.println("******"+TR.getCourseLevel());
                    trainPlan.setProfessionalRequired(TR.getCourseLevel());
                }
                if (TR.getCourseType().equals("专业限选课")) {
//                    System.out.println("******"+TR.getCourseLevel());
                    trainPlan.setLimiteCredit(TR.getCourseLevel());
                }
                if (TR.getCourseType().equals("专业任选课")) {
//                    System.out.println("******"+TR.getCourseLevel());
                    trainPlan.setOptionalCredit(TR.getCourseLevel());
                }
                if (TR.getCourseType().equals("实践环节")) {
//                    System.out.println("******"+TR.getCourseLevel());
                    trainPlan.setPracticeLink(TR.getCourseLevel());
                }

                i++;
            }

            trainPlan.setMajorId(majorName);
            trainPlan.setGrade(grade);
            System.out.println("结果：" + trainPlan.toString());
            System.out.println("indexStart:" + afterIndexStart + "" +
                    "indexEnd:" + afterIndexEnd);

            ImportPlainDao importPlainDao=new ImportPlainDaoImpl();
            importPlainDao.updateAlstrainplancourseByPlain(trainPlan,trainPlans);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    private static String getValue(XSSFCell cell) {
        String value;
        CellType type = cell.getCellType();

        switch (type) {
            case STRING:
                value = cell.getStringCellValue();
                break;

            case BLANK:
                value = "";
                break;
            case BOOLEAN:
                value = cell.getBooleanCellValue() + "";
                break;
            case NUMERIC:
//                //密码和电话bug
//                DecimalFormat df=new DecimalFormat("#");
//                value=df.format(cell.getNumericCellValue());
                value = cell.getNumericCellValue() + "";
                break;
            case FORMULA:
                value = cell.getCellFormula();
                break;
            case ERROR:
                value = "非法字符";
                break;
            default:
                value = "";
                break;
        }
        return value;
    }

}

/*

SQL> desc trainPlanCourseB;
Name               Type          Nullable Default Comments
------------------ ------------- -------- ------- --------
TRAINPLANCOURSE_ID NUMBER(11)
TRAINPLAN_ID       NUMBER(11)    Y
COURSE_CODE        VARCHAR2(20)  Y
COURSE_NAME        VARCHAR2(150) Y
PLAN_COURSE_TERM   NUMBER(2)     Y
CREDIT             NUMBER(3,2)   Y
COURSE_TYPE        VARCHAR2(100) Y
COURSE_HOURS       VARCHAR2(20)  Y
SQL> DESC trainPlanB;
Name                  Type        Nullable Default Comments
--------------------- ----------- -------- ------- --------
TRAINPLAN_ID          NUMBER(11)
MAJOR_ID              NUMBER(11)  Y
GRADE                 INTEGER     Y
OPTIONAL_CREDIT       NUMBER(2,1) Y
LIMITE_CREDIT         NUMBER(2,1) Y
PROFESSIONAL_REQUIRED NUMBER(2,1) Y
GENERAL_REQUIRED      NUMBER(2,1) Y
GENERAL_ELECTIVE      NUMBER(2,1) Y
DISCIPLINE_BASIS      NUMBER(2,1) Y
PRACTICE_LINK         NUMBER(2,1) Y
SQL> DESC majorB;
Name       Type         Nullable Default Comments
---------- ------------ -------- ------- --------
MAJOR_ID   NUMBER(11)
MAJOR_NAME VARCHAR2(60) Y

 */

